package com.zcxy.sys.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxy.core.bean.PageQuery;
import com.zcxy.mybatis.service.BaseServiceImpl;
import com.zcxy.sys.entity.SysAutocodeItem;
import com.zcxy.sys.mapper.SysAutocodeItemMapper;
import com.zcxy.sys.pojo.SysAutocodeItemVo;
import com.zcxy.sys.service.ISysAutocodeItemService;
import com.zcxy.util.BeanCopierUtil;
import com.zcxy.util.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * 用户Service业务层处理
 *
 * @author zcc
 * @email 860474958@qq.com
 * @date 2020-06-01
 */
@Service
@Slf4j
public class SysAutocodeItemServiceImpl extends BaseServiceImpl<SysAutocodeItemMapper, SysAutocodeItem>
        implements ISysAutocodeItemService {



    @Override @Transactional(rollbackFor = Exception.class)
    public IPage<SysAutocodeItemVo> queryPageList(PageQuery<SysAutocodeItem> queryVo) {
        QueryWrapper queryWrapper  = queryVo.getT()!=null?new QueryWrapper(queryVo.getT()):new QueryWrapper();
        Page page = new Page(queryVo.getPageNo(), queryVo.getPageSize());
        IPage<SysAutocodeItem> pageResult = baseMapper.selectPage(page, queryWrapper);
        return BeanCopierUtil.copyPage(pageResult, SysAutocodeItemVo.class);
    }

    @Override @Transactional(rollbackFor = Exception.class)
    public List<SysAutocodeItemVo> queryList() {
        SysAutocodeItem SysAutocodeItem = new SysAutocodeItem();
        QueryWrapper queryWrapper = new QueryWrapper(SysAutocodeItem);
        List<SysAutocodeItem> SysAutocodeItems = baseMapper.selectList(queryWrapper);
        return BeanCopierUtil.copyListToList(SysAutocodeItems, SysAutocodeItemVo.class);
    }

    @Override @Transactional(rollbackFor = Exception.class)
    public void addSysAutocodeItem(SysAutocodeItem SysAutocodeItem) {
        //1.TODO 校验相关数据


        //3.入数据库
        baseMapper.insert(SysAutocodeItem);

    }

    @Override @Transactional(rollbackFor = Exception.class)
    public void updateSysAutocodeItem(SysAutocodeItemVo queryVo) {
        SysAutocodeItem SysAutocodeItemdb = baseMapper.selectById(queryVo.getId());
        BeanCopierUtil.copyObject(queryVo, SysAutocodeItemdb);
        baseMapper.updateById(SysAutocodeItemdb);
    }



    @Override @Transactional(rollbackFor = Exception.class)
    public void removeAllSysAutocodeItem(List<String> ids) {
        QueryWrapper queryWrapper  = new QueryWrapper();
        queryWrapper.in("id", ids);
        baseMapper.delete(queryWrapper);


    }

    @Override
    public void download(SysAutocodeItem SysAutocodeItem, HttpServletResponse response) throws IOException {
        List<SysAutocodeItem> SysAutocodeItems = baseMapper.selectList(new QueryWrapper<>(SysAutocodeItem));
        List<Map<String, Object>> list = new ArrayList<>();
        for (SysAutocodeItem SysAutocodeItemExcel : SysAutocodeItems) {
            Map<String, Object> map = new LinkedHashMap<>();

            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }



}
